package items

import (
	"server/game/cache"
	"server/game/model"
	"server/share/config"

	"github.com/hwcer/cosgo/slice"
	"github.com/hwcer/yyds"
	"github.com/hwcer/yyds/context"
	"github.com/hwcer/yyds/errors"
)

/**
 * @name lock
 * @param string key 物品唯一ID,用','分割。
 * 加锁物品
 */
func (this *items) Lock(c *context.Context) interface{} {
	keys := c.GetString("key")
	if keys == "" {
		return errors.ErrArgEmpty
	}
	arr := slice.Split(keys, ",")
	arr = slice.Unrepeated(arr)
	if l := len(arr); l == 0 || l > 100 {
		return errors.ErrArgEmpty
	}
	coll := cache.GetItems(c.Player.Updater)

	for _, k := range arr {
		d := coll.Get(k)
		if d == nil {
			continue
		}
		if !yyds.Config.ITypes.Is(d.IID, config.ITypeChip, d.IID, config.ITypeUnit) {
			continue
		}
		if d.Attach.GetInt32(model.AttachTypeLocked) == 0 {
			coll.SetAttach(k, model.AttachTypeLocked, int8(1))
		}
	}

	return true
}

/**
 * @name unlockItem
 * @param string key 物品唯一ID,用','分割。
 * 解锁物品
 */

func (this *items) Unlock(c *context.Context) interface{} {
	keys := c.GetString("key")
	if keys == "" {
		return errors.ErrArgEmpty
	}
	arr := slice.Split(keys, ",")
	arr = slice.Unrepeated(arr)
	if l := len(arr); l == 0 || l > 100 {
		return errors.ErrArgEmpty
	}
	coll := cache.GetItems(c.Player.Updater)

	for _, k := range arr {
		d := coll.Get(k)
		if d == nil {
			continue
		}
		if !yyds.Config.ITypes.Is(d.IID, config.ITypeChip, d.IID, config.ITypeUnit) {
			continue
		}
		if d.Attach.GetInt32(model.AttachTypeLocked) > 0 {
			coll.SetAttach(k, model.AttachTypeLocked, int8(0))
		}
	}

	return true
}
